package tij4.concurrent.exercise.ex09;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

class SimplePriorities9ThreadFactory implements ThreadFactory{
    Random random = new Random();
    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r);
        int i = random.nextInt(3);
        switch (i){
            case 0 :
                thread.setPriority(Thread.MIN_PRIORITY);
                break;
            case 1 : thread.setPriority(Thread.NORM_PRIORITY); break;
            case 2 : thread.setPriority(Thread.MAX_PRIORITY); break;
        }
        return thread;
    }
}

public class P09Main01 implements Runnable {
    private int countDown = 5;
    private volatile double d;
    public String toString(){
        return Thread.currentThread() + ": " + countDown;
    }

    @Override
    public void run() {
        while (true){
            for (int i = 0; i < 1000000; i++) {
                d += (Math.PI + Math.E)/(double)i;
                if(i % 1000 == 0)
                    Thread.yield();
            }
            System.out.println(this);
            if (--countDown == 0) return;
        }
    }

    public static void main(String[] args) throws Exception{
        ExecutorService executorService = Executors.newCachedThreadPool(new SimplePriorities9ThreadFactory());
        for (int i = 0; i < 5; i++) {
            executorService.execute(new P09Main01());
        }
        executorService.execute(new P09Main01());
        executorService.shutdown();
    }
}
